//
//  UIView+JwSkiner.m
//  JwPack
//
//  Created by 陈警卫 on 2024-08-06.
//

#import "UIView+JwSkiner.h"
#import <objc/runtime.h>

#import "JwSkinerDeallocer.h"
#import "JwSkiner.h"

static void *kJwSkinerDeallocerKey;
static void *kJwSkinerBlocksKey;

@implementation UIView (JwSkiner)

/// set jw_didSkinerBlock
- (void)setJw_didSkinerBlock:(didJwSkinerBlock)jw_didSkinerBlock {
    objc_setAssociatedObject(self, @selector(jw_didSkinerBlock), jw_didSkinerBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self setupAddSkinerNotier];
}

/// get jw_didSkinerBlock
- (didJwSkinerBlock)jw_didSkinerBlock {
    return objc_getAssociatedObject(self, @selector(jw_didSkinerBlock));
}

/// 添加皮肤观察者
- (void)setupAddSkinerNotier {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kJwSkinerNotiKey object:nil];
    @autoreleasepool {
        id deallocer = objc_getAssociatedObject(self, &kJwSkinerDeallocerKey);
        if (!deallocer) {
            __weak typeof(self) wself = self;
            deallocer = [self setupAddSkinDeallocerBlock:^{
                [[NSNotificationCenter defaultCenter] removeObserver:wself];
            }];
            objc_setAssociatedObject(self, &kJwSkinerDeallocerKey, deallocer, OBJC_ASSOCIATION_ASSIGN);
        }
    }
    
    [self setupSkinNotiChangeAction];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupSkinNotiChangeAction) name:kJwSkinerNotiKey object:nil];
}

/// 添加皮肤观察者处理
- (id)setupAddSkinDeallocerBlock:(void(^)(void))block {
    if (block == nil) {
        return nil;
    }
    NSMutableArray *blocks = objc_getAssociatedObject(self, &kJwSkinerBlocksKey);
    if (blocks == nil) {
        blocks = [NSMutableArray array];
        objc_setAssociatedObject(self, &kJwSkinerBlocksKey, blocks, OBJC_ASSOCIATION_RETAIN);
    }
    
    for (JwSkinerDeallocer *deer in blocks) {
        if (deer.didSkinerDeallocerBlock == block) {
            return nil;
        }
    }
    JwSkinerDeallocer *deer = [JwSkinerDeallocer skinerDeallocerBlock:block];
    [blocks addObject:deer];
    return deer;
}

/// 切换主题通知实现
- (void)setupSkinNotiChangeAction {
    id object = self;
    
    if ([object isKindOfClass:[UIView class]]) {
        UIView *kit = (UIView *)object;
        if (self.jw_backgroundColorKey) {
            kit.backgroundColor = [UIColor jw_colorWithSkinKey:self.jw_backgroundColorKey];
        }
        if (self.jw_tintColorKey) {
            kit.tintColor = [UIColor jw_colorWithSkinKey:self.jw_tintColorKey];
        }
    }
    
    if ([object isKindOfClass:[UILabel class]]) {
        UILabel *kit = (UILabel *)object;
        if (self.jw_textColorKey) {
            kit.textColor = [UIColor jw_colorWithSkinKey:self.jw_textColorKey];
        }
    }
    
    if ([object isKindOfClass:[UITextField class]]) {
        UITextField *kit = (UITextField *)object;
        if (self.jw_textColorKey) {
            kit.textColor = [UIColor jw_colorWithSkinKey:self.jw_textColorKey];
        }
    }
    
    if ([object isKindOfClass:[UIImageView class]]) {
        UIImageView *kit = (UIImageView *)object;
        if (self.jw_imageName) {
            kit.image = [UIImage jw_imageSkinWithName:self.jw_imageName];
        }
    }
    
    
    // 回调对象
    if (self.jw_didSkinerBlock) {
        __weak typeof(self) wself = self;
        self.jw_didSkinerBlock(wself);
    }
}

@end


@implementation UIView (JwSkinerKeys)

/// jw_backgroundColorKey
- (void)setJw_backgroundColorKey:(NSString *)jw_backgroundColorKey {
    objc_setAssociatedObject(self, @selector(jw_backgroundColorKey), jw_backgroundColorKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setupAddSkinerNotier];
}

- (NSString *)jw_backgroundColorKey {
    return objc_getAssociatedObject(self, @selector(jw_backgroundColorKey));
}

/// jw_tintColorKey
- (void)setJw_tintColorKey:(NSString *)jw_tintColorKey {
    objc_setAssociatedObject(self, @selector(jw_tintColorKey), jw_tintColorKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setupAddSkinerNotier];
}

- (NSString *)jw_tintColorKey {
    return objc_getAssociatedObject(self, @selector(jw_tintColorKey));
}

/// jw_textColorKey
- (void)setJw_textColorKey:(NSString *)jw_textColorKey {
    objc_setAssociatedObject(self, @selector(jw_textColorKey), jw_textColorKey, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setupAddSkinerNotier];
}

- (NSString *)jw_textColorKey {
    return objc_getAssociatedObject(self, @selector(jw_textColorKey));
}

/// jw_imageName
- (void)setJw_imageName:(NSString *)jw_imageName {
    objc_setAssociatedObject(self, @selector(jw_imageName), jw_imageName, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    [self setupAddSkinerNotier];
}

- (NSString *)jw_imageName {
    return objc_getAssociatedObject(self, @selector(jw_imageName));
}

@end
